二维数组查找 Posted on 2019-08-25 | In 剑指offer | | reads times 二维数组查找题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 代码 12345678910111213141516171819202122232425262728293031323334353637383940414243var array = [ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8], [5, 6, 7, 8, 9]]//解法一function Find(target, array) { for (var i = 0; i < array.length; i++) { for (var j = 0; j < array[0].length; j++) { if (array[i][j] == target) { return true; } } } return false;}//解法2function Findplus(target,array){ const row=array.length; const col=array[0].length; var rownum=row-1; var colnum=0; if(rownum==0&&colnum==0){ return false; } else{ while(rownum>=0&&colnum<=col-1){ if(array[rownum][colnum]<target){ colnum++; } else if(array[rownum][colnum]>target){ rownum--; } else{ return true; } } }}var a = Find(6, array)console.log(a) Post author: GoldMiner Xun Post link: https://goldminerxun.github.io/2019/08/25/%E5%89%91%E6%8C%87offer%20JavaScript%E7%89%88%20(1)/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.